<template>
    <div>
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/home' }">
                首页</el-breadcrumb-item
            >
            <el-breadcrumb-item :to="{ path: '/page' }">
                权限管理</el-breadcrumb-item
            >
            <el-breadcrumb-item>页面管理</el-breadcrumb-item>
        </el-breadcrumb>
        <el-card class="box-card">
            <el-row>
                <el-col :span="4">
                    <el-input
                        placeholder="请输入菜单名称"
                        v-model="form_data.keyword"
                        clearable
                        @clear="getMenuList"
                        class="input-with-select"
                    >
                        <template #append>
                            <el-button @click="getMenuList">
                                <i class="el-icon-search"></i>
                            </el-button>
                        </template>
                    </el-input>
                </el-col>
                <el-col :span="2">
                    <el-tooltip
                        :enterable="false"
                        effect="dark"
                        content="添加菜单信息"
                        placement="top"
                    >
                        <el-button type="primary" @click="add">
                            <i class="el-icon-circle-plus-outline"></i>
                            添加
                        </el-button>
                    </el-tooltip>
                </el-col>
                <el-col :span="10"
                    ><div class="grid-content bg-purple"></div
                ></el-col>
                <el-col :span="8"
                    ><div class="grid-content bg-purple"></div
                ></el-col>
            </el-row>

            <el-table
                :data="list"
                style="width: 100%; margin-bottom: 15px"
                row-key="id"
                border
            >
                <el-table-column prop="id" label="编号" width="80" />
                <el-table-column label="图标" width="80">
                    <template v-slot="scope">
                        <i :class="[scope.row.icon, 'page_icon']"></i>
                    </template>
                </el-table-column>
                <el-table-column prop="title" label="菜单标题" width="150" />
                <el-table-column prop="name" label="菜单标识" width="120" />
                <el-table-column
                    prop="component_path"
                    label="组件路径地址"
                    width="150"
                />
                <el-table-column prop="sort" label="排序" width="80" />
                <el-table-column
                    prop="create_time"
                    label="创建时间"
                    width="180"
                />
                <el-table-column
                    prop="update_time"
                    label="更新时间"
                    width="180"
                />
                <el-table-column label="菜单类型" width="150">
                    <template v-slot="scope">
                        <el-switch
                            v-model="scope.row.type"
                            :active-value="1"
                            :inactive-value="2"
                            inline-prompt
                            active-color="#13ce66"
                            inactive-color="#e6a23c"
                            active-text="menu"
                            inactive-text="act"
                            @change="updateIsMenu(scope.row)"
                        />
                    </template>
                </el-table-column>
                <el-table-column label="状态" width="70">
                    <template v-slot="scope">
                        <el-switch
                            v-model="scope.row.status"
                            :active-value="1"
                            :inactive-value="0"
                            @change="updateStatus(scope.row)"
                        />
                    </template>
                </el-table-column>
                <el-table-column label="操作">
                    <template v-slot="scope">
                        <el-tooltip
                            :enterable="false"
                            effect="dark"
                            content="编辑页面信息"
                            placement="top"
                        >
                            <el-button
                                type="primary"
                                size="mini"
                                @click="edit(scope.row.id)"
                            >
                                <i class="el-icon-edit"></i>
                            </el-button>
                        </el-tooltip>
                        <el-tooltip
                            :enterable="false"
                            effect="dark"
                            content="删除页面"
                            placement="top"
                        >
                            <el-button
                                type="danger"
                                size="mini"
                                @click="del(scope.row.id)"
                            >
                                <i class="el-icon-delete"></i>
                            </el-button>
                        </el-tooltip>
                        <el-tooltip
                            :enterable="false"
                            effect="dark"
                            content="权限设置"
                            placement="top"
                        >
                            <el-button
                                type="warning"
                                size="mini"
                                @click="setAuth(scope.row.id)"
                            >
                                <i class="el-icon-setting"></i>
                            </el-button>
                        </el-tooltip>
                    </template>
                </el-table-column>
            </el-table>
            <!-- <el-config-provider :locale="locale">
                <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="form_data.page"
                    :page-sizes="[20, 50, 100, 150]"
                    :page-size="form_data.limit"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total"
                >
                </el-pagination>
            </el-config-provider> -->
        </el-card>
    </div>
</template>

<script>
import requestApis from '@/request'
import zhCn from 'element-plus/lib/locale/lang/zh-cn'

export default {
    data () {
        return {
            locale: zhCn,
            list: [],
            total: 0,
            form_data: {
                keyword: '',
                page: 1,
                limit: 20
            }
        }
    },
    created () {
        this.getMenuList()
    },
    methods: {
        add () {
            this.$router.push('/menu/Add')
        },
        edit (id) {
            this.$router.push({ path: '/menu/Edit', query: { id: id } })
        },
        setAuth (id) {
            this.$router.push({ path: '/menu/SetAuth', query: { id: id } })
        },
        del (id) {
            this.$confirm('此操作将永久删除这条记录, 是否继续?', '删除确认', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                requestApis.saveMenuDel({ id }).then(res => {
                    if (res.meta.code === 1) {
                        this.$message({
                            type: 'success',
                            message: res.meta.msg
                        })
                        this.getMenuList()
                    } else {
                        this.$message({
                            message: res.meta.msg,
                            type: 'warning'
                        })
                    }
                }).catch(error => {
                    console.log(error)
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                })
            })
        },
        updateStatus (row) {
            requestApis.saveMenuEdit({ id: row.id, status: row.status }).then(res => {
                if (res.meta.code === 1) {
                    this.getMenuList()
                    this.$message({
                        message: res.meta.msg,
                        type: 'success'
                    })
                } else {
                    this.$message({
                        message: res.meta.msg,
                        type: 'warning'
                    })
                }
            }).catch(error => {
                console.log(error)
            })
        },
        updateIsMenu (row) {
            requestApis.saveMenuEdit({ id: row.id, type: row.type }).then(res => {
                if (res.meta.code === 1) {
                    this.getMenuList()
                    this.$message({
                        message: res.meta.msg,
                        type: 'success'
                    })
                } else {
                    this.$message({
                        message: res.meta.msg,
                        type: 'warning'
                    })
                }
            }).catch(error => {
                console.log(error)
            })
        },
        setLimit (curLimit) {
            this.form_data.limit = curLimit
        },
        getMenuList () {
            var that = this
            requestApis.getTreeMenuAll(this.form_data).then(res => {
                if (res.meta.code === 1) {
                    that.list = res.data
                    console.log(that)
                } else {
                    that.$message({
                        message: res.meta.msg,
                        type: 'warning'
                    })
                }
            }).catch(error => {
                console.log(error)
            })
        },
        handleSizeChange (newLimit) {
            this.form_data.limit = newLimit
            this.getMenuList()
        },
        handleCurrentChange (newPage) {
            this.form_data.page = newPage
            this.getMenuList()
        }
    }

}
</script>

<style lang="stylus" scoped>
.text {
    font-size: 14px;
}

.el-icon-search {
    font-size: 20px;
}

.item {
    padding: 18px 0;
}

.el-table {
    margin-top: 15px;
}

.el-pagination {
    margin-top: 15px;
}

.page_icon {
    font-size: 30px;
}

.input-with-select .el-input-group__prepend {
    background-color: #fff;
}
</style>
